﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class PartiesLookupController : LookupController
    {
        private readonly IRepo<Party> repo;

        public PartiesLookupController(IRepo<Party> repo)
        {
            this.repo = repo;
        }

        public override ActionResult SearchForm()
        {
            return Content("");
        }

        [HttpPost]
        public ActionResult Search(IEnumerable<Guid> selected, Guid? caseId)
        {
            var result = repo.Where(o => true);
            if (caseId.HasValue) result = result.Where(o => o.CaseId == caseId.Value);
            if (selected != null) result = result.Where(o => !selected.Contains(o.Id));
            
            return View(@"Awesome\LookupList", result);
        }

        public ActionResult Selected(IEnumerable<Guid> selected)
        {
            return View(@"Awesome\LookupList", 
                selected == null ? 
                Enumerable.Empty<Party>() : 
                repo.GetAll().Where(o => selected.Contains(o.Id)));
        }

        public ActionResult GetMultiple(IEnumerable<Guid> selected)
        {
            return Json(repo.Where(o => selected.Contains(o.Id)).Select(v => new { Text = v.Person.FirstName +" "+ v.Person.LastName }));
        }
    }
}